|
Involver Developer Network : Chapter 6 - Advanced Contests
This page last changed on Sep 14, 2011 by kristin.bradley@involver.com.
In Chapter 5, we built a fully functioning contest that allows users submit photos with validations, admins can moderate the entries, and we were able to display all approved entries. This chapter will cover advanced functionality - extending the gallery with pagination & voting, adding category filtering & building a standalone canvas view. Adding PaginationThis contest is going to likely receive hundreds of entries so we'll want to paginate the results. Let's add a paginate helper block to help drive this.
{% partial name: "form" %}
{% contest_form %}
<script>
var rules = {
'registration[email]': {required: true},
'submission[celeb]': {required: true},
'submission[before_photo]': {required: true},
'submission[after_photo]': {required: true}
};
</script>
<h4>Celeb Photo Contest Entry Form</h4>
{% unless contest_user_is_registered %}
<div>
Name: <input type="text" name="registration[name]"><br>
Email: <input type="text" name="registration[email]"><br>
</div>
{% endunless %}
<hr>
Celeb: <input type="text" name="submission[celeb]"><br>
Before Photo: {% image_upload_field name:'submission[before_photo]' %}<br>
After Photo: {% image_upload_field name:'submission[after_photo]' %}<br>
<div>
<input type="submit" value="Submit">
</div>
{% endcontest_form %}
{% endpartial %}
{% partial name: "confirmation" %}
<h4>Success!</h4>
<p>Thank you, <strong>{{ contest_user.name }}</strong> for submitting your entry.</p>
<div>
Email: {{ contest_entry.registration.email }}<br>
Celebrity: {{ contest_entry.submission.celeb }}<br>
Before Photo: <img src="{{ contest_entry.submission.before_photo }}"><br>
After Photo: <img src="{{ contest_entry.submission.after_photo }}">
</div>
{% endpartial %}
<h3>Blush Magazine Celeb Photo Contest</h3>
<p>Show us your favorite before and after photos of celebs!</p>
<hr>
{% contest %}
<p>{% contest_form_link entry_form_partial: "form" success_partial: "confirmation" %}
Click here to enter
{% endcontest_form_link %}</p>
{% paginate contest.contest_entries per_page:1 order:"created_at DESC" %}
<ul>
{% for entry in contest_entries %}
<li>
<p><strong>{{ entry.submission.celeb }}</strong>
submitted by {{ entry.contest_user.name }}</p>
<div>
<h3>{{ entry.submission.celeb }}</h3>
{{ entry.submission.before_photo | resize_to: "200" | img_tag }}
{{ entry.submission.after_photo | resize_to: "200" | img_tag }}
</div>
</li>
{% endfor %}
</ul>
{{ pagination_links }}
{% endpaginate %}
{% endcontest %}
We're now showing 1 entry in the gallery at a time with the ability to page through more entries. We've also changed the default ordering of entries in the gallery such that the most recent entries are shown first. This ordering may not be desirable for all users. Furthermore, as our gallery gets larger we'll want to give users more ways to explore submissions. Custom OrderingLet's update our gallery to enable custom ordering. We'll first need to extract our gallery into a partial.
We've made a few changes to our code:
Note that all of these changes were internal, no functionality in our application has changed.
{% partial name: "gallery" %}
{% paginate contest.contest_entries per_page: 3 order: order_by %}
{% for entry in contest_entries %}
<div>
<p><strong>{{ entry.submission.celeb }}</strong>
submitted by {{ entry.contest_user.name }}</p>
<div>
<h3>{{ entry.submission.celeb }}</h3>
{{ entry.submission.before_photo | resize_to: "200" | image_tag }}
{{ entry.submission.after_photo | resize_to: "200" | image_tag }}
</div>
</div>
{% endfor %}
{{ pagination_links }}
{% endpaginate %}
{% endpartial %}
Late-Binding and ajax_linkPassing in the value of the contest & order_by variables in our render tag is an example of late-binding. Late-binding allows you to specify the values of variables at a later time. Using late-binding with the render tag may not seem that special, we're still providing the values up front, we've just restructured our code to render the gallery as a partial. However, there is an additional way to render a partial: using the ajax_link tag. The ajax_link tag enables you to render a partial whenever a user clicks on a link. Late-binding combined with the ajax_link tag is a very powerful facility. We are no longer limited to rendering SML once at page load, but at any point in our application. Using this facility you can build sophisticated functionality such as custom ordering, which we'll now introduce in our contest:
Let's take a minute to break down the code changes:
|
| Document generated by Confluence on Feb 12, 2013 09:09 |